package com.ysd.dao.impl;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import com.sun.org.apache.xalan.internal.xsltc.compiler.sym;
import com.ysd.entity.Designer;
import com.ysd.util.DBManager;
//瀹炵幇绫�
public class DesignerDaolmpl {
	//鏌ヨ鎵�鏈変骇鍝佸垎绫�
	//璁块棶淇グ绗� 杩斿洖鍊肩被鍨� 鏂规硶鍚嶏紙鍙傛暟鍒楄〃锛墈鏂规硶浣搣
		public List<Designer> getDesignerList(){
			List<Designer> designerList=new ArrayList<Designer>();
			Connection conn=DBManager.getConnection();
			Statement st=null;
			ResultSet rs=null;
			
			try {
				st=conn.createStatement();
				rs=st.executeQuery("select * from designer");
				while (rs.next()) {
					int dId=rs.getInt("designerId");
					String dName=rs.getString("designerName");
					String dTitle=rs.getString("designerTitle");
					String dImg=rs.getString("designerImg");
					String dCon=rs.getString("designerContent");
					String dCi=rs.getString("designerContentImg");
					String dCc=rs.getString("designerCompanyContent");
					String deCi=rs.getString("designerCompanyImg");
					Designer designer=new Designer(dId,dName,dTitle,dImg,dCon,dCi,dCc,deCi);
					designerList.add(designer);
					
				}
				return designerList;
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return null;
				
			}finally {
				DBManager.closeAll(conn, st, rs);
			}
		}
	/**
	 * 分页查询*/
		public List<Designer> getDesignerByPage(int page,int size){
			List<Designer> designerList=new ArrayList<Designer>();
			Connection conn=DBManager.getConnection();
			Statement st=null;
			ResultSet rs=null;
			
			try {
				st=conn.createStatement();
				String sql="select * from designer limit "+(page-1)*size+","+size;
				System.out.println(sql);
				rs=st.executeQuery(sql);
				while (rs.next()) {
					int dId=rs.getInt("designerId");
					String dName=rs.getString("designerName");
					String dTitle=rs.getString("designerTitle");
					String dImg=rs.getString("designerImg");
					String dCon=rs.getString("designerContent");
					String dCi=rs.getString("designerContentImg");
					String dCc=rs.getString("designerCompanyContent");
					String deCi=rs.getString("designerCompanyImg");
					Designer designer=new Designer(dId,dName,dTitle,dImg,dCon,dCi,dCc,deCi);
					designerList.add(designer);
					
				}
				return designerList;
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return null;
				
			}finally {
				DBManager.closeAll(conn, st, rs);
			}
		}
		public int delDesignerById(int delId) {
			return DBManager.executeUpdate("DELETE from designer where designerId="+delId);
		}
		/**
		 * 娣诲姞璁捐甯堝垎绫�*/
		public int addDesigner(Designer de) {
			return DBManager.executeUpdate("insert into designer values (null,'"+de.getDesignerName()+"','"+de.getDesignerTitle()+"','"+de.getDesignerImg()+"','"+de.getDesignerContent()+"','"+de.getDesignerContentImg()+"','"+de.getDesignerCompanyContent()+"','"+de.getDesignerCompanyImg()+"')");
		}
		
		/**
		 * */
		public Designer getDesignerbById(int deId) {
		Connection conn=DBManager.getConnection();
		Statement st=null;
		Designer designer=null;
		ResultSet rs=null;
		try {
			st=conn.createStatement();
			rs=st.executeQuery("SELECT *from designer where designerId="+deId);
			while(rs.next()) {
				int dId=rs.getInt("designerId");
				String dName=rs.getString("designerName");
				String dTitle=rs.getString("designerTitle");
				String dImg=rs.getString("designerImg");
				String dCon=rs.getString("designerContent");
				String dCi=rs.getString("designerContentImg");
				String dCc=rs.getString("designerCompanyContent");
				String deCi=rs.getString("designerCompanyImg");
				designer=new Designer(dId,dName,dTitle,dImg,dCon,dCi,dCc,deCi);
				
			}
			return designer;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}finally {
			DBManager.closeAll(conn, st, rs);
		}
		
		}
		
		public int updateDesigner(Designer d) {
			String sql = "update designer set designerName='"+d.getDesignerName()+"'," + 
					"designerTitle='"+d.getDesignerTitle()+"'," + 
					"designerImg='"+d.getDesignerImg()+"'," + 
					"designerContent='"+d.getDesignerContent()+"'," + 
					"designerContentImg='"+d.getDesignerContentImg()+"'," + 
					"designerCompanyContent='"+d.getDesignerCompanyContent()+"'," + 
					"designerCompanyImg='"+d.getDesignerCompanyImg()+"'" + 
					" where designerId="+d.getDesignerId();
			System.out.println(sql);
			return DBManager.executeUpdate(sql);
		}
		
		/**
		 * 批量删除
		 */
		public int deleteDesignerById(String desId) {
			return DBManager.executeUpdate("delete from designer where designerId in("+desId+")");
		}
}
